package lesson.tencentapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lesson.entity.ChinaTotal;
import lesson.entity.NocvData;
import lesson.service.ChinaTotalService;
import lesson.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: Wanzhen
 * @Date: 2022/6/12-11:54
 */
@Component
public class ChinaTotalScheduleTask {

    @Autowired
    private ChinaTotalService chinaTotalService;

    @Autowired
    private IndexService indexService;

    //定义每12小时执行一次，全国数据情况
    @Scheduled(fixedDelay = 43200000) //12h执行一次
    //@Scheduled(cron = "0 0 12")  //12点获取一次
    public void updateChinaTotalToDB() throws Exception {
        HttpUtils httpUtils = new HttpUtils();
        String string = httpUtils.getData();
        //System.out.println("原始数据：" + string );
        System.out.println("***********************************************");

        //1.所有数据的alibaba格式
        JSONObject jsonObject = JSONObject.parseObject(string);
        Object data = jsonObject.get("data");
        //System.out.println("data:" + data);
        System.out.println("***********************************************");

        //2.data类型
        JSONObject jsonObjectData = JSONObject.parseObject(data.toString());
        Object chinaTotal = jsonObjectData.get("chinaTotal");
        Object overseaLastUpdateTime = jsonObjectData.get("overseaLastUpdateTime"); //更新时间
        //System.out.println("chinaTotal:" + chinaTotal);
        System.out.println("***********************************************");

        //3.total 全中国整体疫情数据
        JSONObject jsonObjectTotal = JSONObject.parseObject(chinaTotal.toString());
        Object total = jsonObjectTotal.get("total");
        //System.out.println("total:" + total);
        System.out.println("***********************************************");

        //4.全国数据total
        JSONObject totalData = JSONObject.parseObject(total.toString());
        Object confirm = totalData.get("confirm");  //全国总确诊人数
        Object input = totalData.get("input");
        Object severe = totalData.get("severe");
        Object heal = totalData.get("heal");
        Object dead = totalData.get("dead");
        Object suspect = totalData.get("suspect");

        //5.为程序实体赋值
        ChinaTotal dataEntity = new ChinaTotal();
        dataEntity.setConfirm((Integer) confirm);
        dataEntity.setInput((Integer) input);
        dataEntity.setSevere((Integer) severe);
        dataEntity.setHeal((Integer) heal);
        dataEntity.setDead((Integer) dead);
        dataEntity.setSuspect((Integer) suspect);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        dataEntity.setUpdateTime(format.parse(String.valueOf(overseaLastUpdateTime)));

        //6.录入数据库(增加)
        chinaTotalService.save(dataEntity);

        System.out.println("+++++++++++++++++++++++++++++++++++++");
        System.out.println("-------------------------------------");

        //7.中国各省份数据的自动刷新
        //7.1拿到areaTree
        JSONArray areaTree = jsonObjectData.getJSONArray("areaTree");
        Object[] objects = areaTree.toArray();
        //7.2遍历所有的国家的数据
        /*for(int i = 0;i < objects.length;i++){
            JSONObject jsonObject1 = JSONObject.parseObject(objects[i].toString());
            Object name = jsonObject1.get("name");
            System.out.println(name);
        }*/
        //7.2拿到中国的数据
        JSONObject jsonObject1 = JSONObject.parseObject(objects[2].toString());
        JSONArray children = jsonObject1.getJSONArray("children");// json格式各个省份
        Object[] objects1 = children.toArray();// 各个省份

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        List<NocvData> list = new ArrayList<>();

        for(int i = 0;i < objects1.length;i++){
            NocvData nocvData = new NocvData();

            JSONObject jsonObject2 = jsonObject1.parseObject(objects1[i].toString());
            Object provinceName = jsonObject2.get("name"); //省份名字
            Object provinceUpdateTime = jsonObject2.get("lastUpdateTime"); //省份更新数据时间
            Object provinceTotal = jsonObject2.get("total"); //省份数据
            JSONObject jsonObject3 = JSONObject.parseObject(provinceTotal.toString());
            Object confirm1 = jsonObject3.get("confirm");//各省份累计确诊人数

            //获取累计死亡人数和治愈人数
            Object heal1 = jsonObject3.get("heal");
            Object dead1 = jsonObject3.get("dead");

            //现存确诊人数
            int currentConfirm = Integer.parseInt((confirm1.toString())) -
                                 Integer.parseInt((heal1.toString())) -
                                 Integer.parseInt((dead1.toString())) ;

            //System.out.println("省份->" + provinceName + ":" + confirm1 + "人");

            //赋值
            nocvData.setName(provinceName.toString());
            nocvData.setValue(currentConfirm);
            if(provinceUpdateTime == null){
                nocvData.setUpdateTime(new Date());
            }else {
                nocvData.setUpdateTime(format1.parse(String.valueOf(provinceUpdateTime)));
            }
            list.add(nocvData);
        }
        //7.3各个省份的数据插入数据库
        indexService.saveBatch(list);

        //8、删除缓存mysql--redis已执行的一个简单套路
        Jedis jedis = new Jedis("127.0.0.1");
        //客户端连上去了，才去删除redis中的缓存
        if(jedis != null){
            jedis.flushDB(); //注意：只删除自己使用的键值对就好
        }

    }

}
